﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Reflection;
using System.Text;
using log4net;

namespace EasyDLL.Logging
{
    /// <summary>
    /// 日志记录类，使用此类请在项目中添加log4net的引用。
    ///  使用web项目时候请将log4net.config复制到网站根目录，winform项目则将其复制到与执行文件同目录。
    /// <br />
    /// 【写日志的原则】
    /// Ⅰ.在catch后,把异常写入日志.
    /// Ⅱ.在调用第三方控件的开始和结束处.
    /// Ⅲ.在连接数据库的开始结束处.
    /// Ⅳ.除非必要,不要在循环体中加入日志,否则一旦出问题可能导致日志暴增.
    /// Ⅴ.在自己认为很重要的逻辑处写入日志.
    /// </summary>
    /// <remarks>
    /// 参考：http://www.cnblogs.com/qianlifeng/archive/2011/04/22/2024856.html
    /// </remarks>
    public class LoggingWrapper
    {
        private static LoggingWrapper instance;
        public static LoggingWrapper Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new LoggingWrapper();
                }
                return instance;
            }
        }

        /// <summary>
        /// 记录一般信息
        /// </summary>
        /// <param name="msg"></param>
        public void LogInfo(string msg)
        {
            ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
            log.Info(msg);
        }

        /// <summary>
        /// 记录一个debug信息
        /// </summary>
        /// <param name="msg"></param>
        public void LogDebug(string msg)
        {
            ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
            log.Debug(msg);
          
        }

        /// <summary>
        /// 记录一个error信息
        /// </summary>
        /// <param name="msg"></param>
        public void LogError(string msg)
        {
            ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
            log.Error(msg);
          
        }

        /// <summary>
        /// 记录一个Fatal（致命的）信息
        /// </summary>
        /// <param name="msg"></param>
        public void LogFatal(string msg)
        {
            ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
            log.Fatal(msg);
        
        }

        /// <summary>
        /// 记录一个Warn信息
        /// </summary>
        /// <param name="msg"></param>
        public void LogWarn(string msg)
        {
            ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
            log.Warn(msg);
        }
    }
}
